package com.csxy.systemmanager.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串数组转为List<Long>
 */
public class ListLongJsonDeserializer extends JsonDeserializer<List<Long>> {

    @Override
    public List<Long> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
        CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, String.class);
        List<String> strList = mapper.readValue(jsonParser, collectionType);
        return strList.stream().map(Long::parseLong).collect(Collectors.toList());
    }
}